; boot.asm - MBR引导扇区，加载loader到0x9000:0x0000并跳转
org 0x7C00

section .text
start:
    cli                   ; 关中断
    xor ax, ax
    mov ds, ax
    mov ss, ax
    mov sp, 0x7C00
    mov si, msg
    call print

    ; 读loader到0x9000:0x0000 (加载1个扇区)
    mov ax, 0x9000
    mov es, ax
    xor bx, bx
    mov ah, 0x02     ; 读取扇区
    mov al, 1        ; 读1个扇区
    mov ch, 0        ; 柱面0
    mov cl, 2        ; 扇区2（MBR占用1扇区）
    mov dh, 0        ; 磁头0
    mov dl, 0x80     ; 硬盘
    int 0x13
    jc disk_error

    jmp 0x9000:0x0000 ; 跳转到loader

disk_error:
    mov si, disk_err_msg
    call print
    hlt

print:                ; BIOS打印字符串，SI=字符串地址
    mov ah, 0x0E
.next:
    lodsb
    or al, al
    jz .done
    int 0x10
    jmp .next
.done:
    ret

msg db "Boot to loader...", 13, 10, 0
disk_err_msg db "Disk read error!", 13, 10, 0

times 510-($-$$) db 0
dw 0xAA55